#!/usr/bin/env python

import sys, os, pygtk
pygtk.require('2.0')
import gtk

class MainWin:
	def __init__(self):
		self.mEventLock = False

		win = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.mMainWin = win
		win.set_border_width(10)
		win.set_title("Cavan configure")
		win.resize(350, 100)
		win.connect("destroy", self.OnWinDestroy)

		vbox = gtk.VBox(False, 2)
		win.add(vbox)

		frame = gtk.Frame("General Option")
		vbox.pack_start(frame, False, False, 0)

		table = gtk.Table(5, 2, False)
		frame.add(table)

		combo_style = gtk.EXPAND | gtk.FILL

		table.attach(gtk.Label("Architecture:"), 0, 1, 0, 1, 0, 0, 5, 5)
		combo = gtk.combo_box_new_text()
		combo.append_text("host")
		combo.append_text("arm")
		combo.append_text("x86")
		combo.set_active(0)
		combo.connect("changed", self.OnArchChanged)
		table.attach(combo, 1, 2, 0, 1, combo_style, combo_style, 5, 5)
		self.mComboArch = combo

		table.attach(gtk.Label("Toolchian:"), 0, 1, 1, 2, combo_style, combo_style, 5, 5)
		combo = gtk.combo_box_entry_new_text()
		# entry = combo.get_child()
		combo.append_text("none")
		combo.append_text("arm-linux")
		combo.append_text("arm-cavan-linux-gnueabi")
		combo.append_text("arm-none-linux-gnueabi")
		combo.set_active(0)
		combo.connect("changed", self.OnCrossCompileChanged)
		self.mComboCrossCompile = combo
		table.attach(combo, 1, 2, 1, 2, combo_style, combo_style, 5, 5)

		table.attach(gtk.Label("Build Type:"), 0, 1, 2, 3, combo_style, combo_style, 5, 5)
		combo = gtk.combo_box_new_text()
		combo.append_text("debug")
		combo.append_text("release")
		combo.append_text("static")
		combo.set_active(0)
		combo.connect("changed", self.OnBuildTypeChanged)
		self.mComboBuildType = combo
		table.attach(combo, 1, 2, 2, 3, combo_style, combo_style, 5, 5)

		table.attach(gtk.Label("Build Entry:"), 0, 1, 3, 4, combo_style, combo_style, 5, 5)
		combo = gtk.combo_box_new_text()
		combo.append_text("application")
		combo.append_text("library")
		combo.append_text("cavan")
		combo.set_active(0)
		self.mComboBuildEntry = combo
		table.attach(combo, 1, 2, 3, 4, combo_style, combo_style, 5, 5)

		table.attach(gtk.Label("Build Others:"), 0, 1, 4, 5, combo_style, combo_style, 5, 5)
		combo = gtk.combo_box_new_text()
		combo.append_text("false")
		combo.append_text("true")
		combo.set_active(0)
		self.mComboBuildOthers = combo
		table.attach(combo, 1, 2, 4, 5, combo_style, combo_style, 5, 5)

		frame = gtk.Frame("Compile Option")
		vbox.pack_start(frame, False, False, 10)

		table = gtk.Table(2, 2, False)
		frame.add(table)

		table.attach(gtk.Label("CFLAGS:"), 0, 1, 0, 1, combo_style, combo_style, 5, 5)
		entry = gtk.Entry()
		self.mEntryCflags = entry
		table.attach(entry, 1, 2, 0, 1, combo_style, combo_style, 5, 5)

		table.attach(gtk.Label("LDFLAGS:"), 0, 1, 1, 2, combo_style, combo_style, 5, 5)
		entry = gtk.Entry()
		self.mEntryLdflags = entry
		table.attach(entry, 1, 2, 1, 2, combo_style, combo_style, 5, 5)

		hbox = gtk.HBox(False, 0)
		vbox.pack_start(hbox, False, False, 0)

		button = gtk.Button("Build")
		button.set_size_request(100, 30)
		button.connect("clicked", self.OnBuildClicked)
		hbox.pack_start(button, True, False, 0)
		button.grab_focus()

		button = gtk.Button("Cancel")
		button.set_size_request(100, 30)
		button.connect("clicked", self.OnCancelClicked)
		hbox.pack_start(button, True, False, 0)

		separator = gtk.HSeparator()
		vbox.pack_start(separator, False, True, 10)

		win.show_all()

	def GetCavanRoot(self):
		script_path = os.path.dirname(sys.argv[0])
		return os.path.dirname(script_path)

	def GenMakefile(self):
		lines = []
		lines.append("ARCH = %s\n" % self.mComboArch.get_active_text())

		cross_compile = self.mComboCrossCompile.get_active_text()
		if cross_compile == "none" or cross_compile == "":
			lines.append("CROSS_COMPILE =\n")
		else:
			lines.append("CROSS_COMPILE = %s-\n" % cross_compile.rstrip("-"))

		build_entry = self.mComboBuildEntry.get_active_text()
		if build_entry == "application":
			build_entry = "app"
		elif build_entry == "library":
			build_entry = "lib"
		else:
			build_entry = "cavan"

		build_others = self.mComboBuildOthers.get_active_text()
		if build_others == "true":
			ldflag_others = "-lasound"
		else:
			ldflag_others = ""

		lines.append("BUILD_TYPE = %s\n" % self.mComboBuildType.get_active_text())
		lines.append("BUILD_ENTRY = %s\n" % build_entry)
		lines.append("BUILD_OTHERS = %s\n" % build_others)
		lines.append("CFLAGS = %s\n" % self.mEntryCflags.get_text())
		lines.append("LDFLAGS = %s %s\n" % (ldflag_others, self.mEntryLdflags.get_text()))
		lines.append("Q = @\n")
		lines.append("CONFIG_CAVAN_SSL = true\n")
		lines.append("\ninclude build/core/main3.mk")

		if len(sys.argv) > 1:
			makefile_path = sys.argv[1]
		else:
			makefile_path = os.path.join(self.GetCavanRoot(), "Makefile")

		print "Writing file %s" % makefile_path

		fd = file(makefile_path, "w")
		fd.writelines(lines)
		fd.close()
		gtk.main_quit()

	def EventLockDown(self):
		if self.mEventLock:
			return -1
		self.mEventLock = True
		return 0

	def EventLockUp(self):
		self.mEventLock = False

	def OnBuildClicked(self, widget, data = None):
		self.GenMakefile()

	def OnCancelClicked(self, widget, data = None):
		gtk.main_quit()

	def OnWinDestroy(self, widget, data = None):
		gtk.main_quit()

	def SetComboActiveText(self, combo, text):
		active = 0
		for model in combo.get_model():
			if model[0] == text:
				combo.set_active(active)
				return 0
			active = active + 1
		return -1

	def OnArchChanged(self, widget):
		if self.EventLockDown() < 0:
			return -1
		arch = widget.get_active_text()
		if arch == "arm":
			toolchian = "arm-cavan-linux-gnueabi"
			type = "static"
			entry = "cavan"
		else:
			toolchian = "none"
			type = "debug"
			entry = "application"
		self.SetComboActiveText(self.mComboCrossCompile, toolchian)
		self.SetComboActiveText(self.mComboBuildType, type);
		self.SetComboActiveText(self.mComboBuildEntry, entry);
		self.EventLockUp()

	def OnBuildTypeChanged(self, widget):
		if self.EventLockDown() < 0:
			return -1
		arch = widget.get_active_text()
		if arch == "static":
			entry = "cavan"
		else:
			entry = "application"
		self.SetComboActiveText(self.mComboBuildEntry, entry);
		self.EventLockUp()

	def OnCrossCompileChanged(self, widget):
		if self.EventLockDown() < 0:
			return -1
		toolchian = widget.get_active_text();
		if toolchian.find("arm", 0, 3) == 0:
			arch = "arm"
		elif toolchian == "none":
			arch = "host"
		else:
			arch = None

		if arch != None:
			self.SetComboActiveText(self.mComboArch, arch)

		self.EventLockUp()

	def MainLoop(self):
		gtk.main()

if __name__ == "__main__":
	win = MainWin()
	win.MainLoop()
